package timer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;

/**
 * Created by Ming on 2017/6/3.
 */
public class MyTimer {
    public static void main(String[] args) {
        //1.创建一个timer实例
        Timer timer = new Timer();
        //2. 创建mytimertask实例
        MytTimerTask mytTimerTask = new MytTimerTask("No.1");
        //3 通过timer定时定频率调用mytimertask的业务逻辑
        //即第一次执行是在当前时间的两秒后, 每隔一秒执行一次
//        timer.schedule(mytTimerTask, 2000L, 1000L);

        //获取当前时间,并设置成距离当前时间三秒后执行任务
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(calendar.getTime()));
        calendar.add(Calendar.SECOND, 3);
//        mytTimerTask.setName("schedule1");
//        timer.schedule(mytTimerTask, calendar.getTime());

//        mytTimerTask.setName("schedule2");
//        timer.schedule(mytTimerTask, calendar.getTime(), 2000L);

//        mytTimerTask.setName("schedule3");
//        timer.schedule(mytTimerTask, 2000L);

//        mytTimerTask.setName("schedule4");
//        timer.schedule(mytTimerTask, 2000L, 2000L);

//        mytTimerTask.setName("schedule at fixedrate 1");
//        timer.scheduleAtFixedRate(mytTimerTask, calendar.getTime(), 2000L);

//        mytTimerTask.setName("schedule at fixedrate 2");
//        timer.scheduleAtFixedRate(mytTimerTask, 2000L, 2000L);

        mytTimerTask.setName("schedule3");
        timer.schedule(mytTimerTask, 2000L);
        System.out.println(simpleDateFormat.format(mytTimerTask.scheduledExecutionTime()));
    }
}
